/*********************************************************************************************************************
* MM32F527X-E9P Opensourec Library 鍗筹紙MM32F527X-E9P 寮€婧愬簱锛夋槸涓€涓熀浜庡畼鏂� SDK 鎺ュ彛鐨勭涓夋柟寮€婧愬簱
* Copyright (c) 2022 SEEKFREE 閫愰绉戞妧
* 
* 鏈枃浠舵槸 MM32F527X-E9P 寮€婧愬簱鐨勪竴閮ㄥ垎
* 
* MM32F527X-E9P 寮€婧愬簱 鏄厤璐硅蒋浠�
* 鎮ㄥ彲浠ユ牴鎹嚜鐢辫蒋浠跺熀閲戜細鍙戝竷鐨� GPL锛圙NU General Public License锛屽嵆 GNU閫氱敤鍏叡璁稿彲璇侊級鐨勬潯娆�
* 鍗� GPL 鐨勭3鐗堬紙鍗� GPL3.0锛夋垨锛堟偍閫夋嫨鐨勶級浠讳綍鍚庢潵鐨勭増鏈紝閲嶆柊鍙戝竷鍜�/鎴栦慨鏀瑰畠
* 
* 鏈紑婧愬簱鐨勫彂甯冩槸甯屾湜瀹冭兘鍙戞尌浣滅敤锛屼絾骞舵湭瀵瑰叾浣滀换浣曠殑淇濊瘉
* 鐢氳嚦娌℃湁闅愬惈鐨勯€傞攢鎬ф垨閫傚悎鐗瑰畾鐢ㄩ€旂殑淇濊瘉
* 鏇村缁嗚妭璇峰弬瑙� GPL
* 
* 鎮ㄥ簲璇ュ湪鏀跺埌鏈紑婧愬簱鐨勫悓鏃舵敹鍒颁竴浠� GPL 鐨勫壇鏈�
* 濡傛灉娌℃湁锛岃鍙傞槄<https://www.gnu.org/licenses/>
* 
* 棰濆娉ㄦ槑锛�
* 鏈紑婧愬簱浣跨敤 GPL3.0 寮€婧愯鍙瘉鍗忚 浠ヤ笂璁稿彲鐢虫槑涓鸿瘧鏂囩増鏈�
* 璁稿彲鐢虫槑鑻辨枃鐗堝湪 libraries/doc 鏂囦欢澶逛笅鐨� GPL3_permission_statement.txt 鏂囦欢涓�
* 璁稿彲璇佸壇鏈湪 libraries 鏂囦欢澶逛笅 鍗宠鏂囦欢澶逛笅鐨� LICENSE 鏂囦欢
* 娆㈣繋鍚勪綅浣跨敤骞朵紶鎾湰绋嬪簭 浣嗕慨鏀瑰唴瀹规椂蹇呴』淇濈暀閫愰绉戞妧鐨勭増鏉冨０鏄庯紙鍗虫湰澹版槑锛�
* 
* 鏂囦欢鍚嶇О          zf_device_mt9v03x
* 鍏徃鍚嶇О          鎴愰兘閫愰绉戞妧鏈夐檺鍏徃
* 鐗堟湰淇℃伅          鏌ョ湅 libraries/doc 鏂囦欢澶瑰唴 version 鏂囦欢 鐗堟湰璇存槑
* 寮€鍙戠幆澧�          MDK 5.37
* 閫傜敤骞冲彴          MM32F527X_E9P
* 搴楅摵閾炬帴          https://seekfree.taobao.com/
* 
* 淇敼璁板綍
* 鏃ユ湡              浣滆€�                澶囨敞
* 2022-08-10        Teternal            first version
********************************************************************************************************************/
/*********************************************************************************************************************
* 鎺ョ嚎瀹氫箟锛�
*                   ------------------------------------
*                   妯″潡绠¤剼            鍗曠墖鏈虹鑴�
*                   TXD                 鏌ョ湅 zf_device_mt9v03x.h 涓� MT9V03X_COF_UART_TX 瀹忓畾涔�
*                   RXD                 鏌ョ湅 zf_device_mt9v03x.h 涓� MT9V03X_COF_UART_RX 瀹忓畾涔�
*                   PCLK                鏌ョ湅 zf_device_mt9v03x.h 涓� MT9V03X_PCLK_PIN 瀹忓畾涔�
*                   VSY                 鏌ョ湅 zf_device_mt9v03x.h 涓� MT9V03X_VSYNC_PIN 瀹忓畾涔�
*                   D0-D7               鏌ョ湅 zf_device_mt9v03x.h 涓� MT9V03X_DATA_PIN 瀹忓畾涔� 浠庤瀹氫箟寮€濮嬬殑杩炵画鍏釜寮曡剼
*                   VCC                 3.3V鐢垫簮
*                   GND                 鐢垫簮鍦�
*                   鍏朵綑寮曡剼鎮┖
*                   ------------------------------------
********************************************************************************************************************/

#ifndef _zf_device_mt9v03x_h_
#define _zf_device_mt9v03x_h_

#include "zf_common_typedef.h"

//--------------------------------------------------------------------------------------------------
// 寮曡剼閰嶇疆
//--------------------------------------------------------------------------------------------------
#define MT9V03X_COF_UART        (UART_5)                                        // 閰嶇疆鎽勫儚澶存墍浣跨敤鍒扮殑涓插彛
#define MT9V03X_COF_BAUR        (9600)                                          // 鎬婚捇椋庨厤缃覆鍙ｆ尝鐗圭巼
#define MT9V03X_COF_UART_TX     (UART5_RX_D2)                                   // 鎬婚捇椋� UART-TX 寮曡剼 瑕佹帴鍦ㄥ崟鐗囨満 RX 涓�
#define MT9V03X_COF_UART_RX     (UART5_TX_C12)                                  // 鎬婚捇椋� UART-RX 寮曡剼 瑕佹帴鍦ㄥ崟鐗囨満 TX 涓�

#define MT9V03X_COF_IIC_DELAY   (150)                                           // 鎬婚捇椋� IIC 寤舵椂
#define MT9V03X_COF_IIC_SCL     (D2 )                                           // 鎬婚捇椋� IIC-SCL 寮曡剼
#define MT9V03X_COF_IIC_SDA     (C12)                                           // 鎬婚捇椋� IIC-SDA 寮曡剼

#define MT9V03X_DMA_CH          (DMA1_CHANNEL4)
#define MT9V03X_DMA_IRQN        (DMA1_CH4_IRQn)

#define MT9V03X_PCLK_PIN        (TIM1_ETR_E7)                                   // PCLK 瑙﹀彂淇″彿 TIM_ETR 寮曡剼绂佹闅忔剰淇敼

#define MT9V03X_VSYNC_PIN       (E8 )                                           // 鍦轰腑鏂紩鑴�
#define MT9V03X_VSYNC_IRQN      (EXTI9_5_IRQn)                                  // 涓柇鍙�

#define MT9V03X_DATA_PIN        (G0)                                            // 鏁版嵁寮曡剼 杩欓噷鏄� 鍙兘鏄� GPIOx0 鎴栬€� GPIOx8 寮€濮� 杩炵画鍏釜寮曡剼渚嬪 F0-F7
#define MT9V03X_DATA_ADD        (gpio_idr_addr(MT9V03X_DATA_PIN))

#define MT9V03X_INIT_TIMEOUT    (0x0080)                                        // 榛樿鐨勬憚鍍忓ご鍒濆鍖栬秴鏃舵椂闂� 姣涓哄崟浣�

//--------------------------------------------------------------------------------------------------
// 鎽勫儚澶撮粯璁ゅ弬鏁伴厤缃� 鍦ㄦ淇敼鎽勫儚澶撮厤缃�
//--------------------------------------------------------------------------------------------------
#define MT9V03X_W               (188)                                           // 鍥惧儚瀹藉害     鑼冨洿 [1-752]
#define MT9V03X_H               (120)                                           // 鍥惧儚楂樺害     鑼冨洿 [1-480]
#define MT9V03X_IMAGE_SIZE      (MT9V03X_W * MT9V03X_H)                         // 鏁翠綋鍥惧儚澶у皬涓嶈兘瓒呰繃 65535

#define MT9V03X_AUTO_EXP_DEF    (0  )                                           // 鑷姩鏇濆厜璁剧疆     榛樿涓嶅紑鍚嚜鍔ㄦ洕鍏夎缃�  鑼冨洿 [0-63] 0涓哄叧闂�
                                                                                //                  濡傛灉鑷姩鏇濆厜寮€鍚�  EXP_TIME鍛戒护璁剧疆鑷姩鏇濆厜鏃堕棿鐨勪笂闄�
                                                                                //                  涓€鑸儏鍐垫槸涓嶉渶瑕佸紑鍚嚜鍔ㄦ洕鍏夎缃� 濡傛灉閬囧埌鍏夌嚎闈炲父涓嶅潎鍖€鐨勬儏鍐靛彲浠ュ皾璇曡缃嚜鍔ㄦ洕鍏夛紝澧炲姞鍥惧儚绋冲畾鎬�
#define MT9V03X_EXP_TIME_DEF    (512)                                           // 鏇濆厜鏃堕棿         鎽勫儚澶存敹鍒板悗浼氳嚜鍔ㄨ绠楀嚭鏈€澶ф洕鍏夋椂闂达紝濡傛灉璁剧疆杩囧ぇ鍒欒缃负璁＄畻鍑烘潵鐨勬渶澶ф洕鍏夊€�
#define MT9V03X_FPS_DEF         (50 )                                           // 鍥惧儚甯х巼         鎽勫儚澶存敹鍒板悗浼氳嚜鍔ㄨ绠楀嚭鏈€澶PS锛屽鏋滆繃澶у垯璁剧疆涓鸿绠楀嚭鏉ョ殑鏈€澶PS
#define MT9V03X_LR_OFFSET_DEF   (0  )                                           // 鍥惧儚宸﹀彸鍋忕Щ閲�   姝ｅ€� 鍙冲亸绉�   璐熷€� 宸﹀亸绉�  鍒椾负188 376 752鏃舵棤娉曡缃亸绉�
                                                                                //                  鎽勫儚澶存敹鍋忕Щ鏁版嵁鍚庝細鑷姩璁＄畻鏈€澶у亸绉伙紝濡傛灉瓒呭嚭鍒欒缃绠楀嚭鏉ョ殑鏈€澶у亸绉�
#define MT9V03X_UD_OFFSET_DEF   (0  )                                           // 鍥惧儚涓婁笅鍋忕Щ閲�   姝ｅ€� 涓婂亸绉�   璐熷€� 涓嬪亸绉�  琛屼负120 240 480鏃舵棤娉曡缃亸绉�
                                                                                //                  鎽勫儚澶存敹鍋忕Щ鏁版嵁鍚庝細鑷姩璁＄畻鏈€澶у亸绉伙紝濡傛灉瓒呭嚭鍒欒缃绠楀嚭鏉ョ殑鏈€澶у亸绉�
#define MT9V03X_GAIN_DEF        (32 )                                           // 鍥惧儚澧炵泭         鑼冨洿 [16-64]  澧炵泭鍙互鍦ㄦ洕鍏夋椂闂村浐瀹氱殑鎯呭喌涓嬫敼鍙樺浘鍍忎寒鏆楃▼搴�
#define MT9V03X_PCLK_MODE_DEF   (0  )                                           // 鍍忕礌鏃堕挓妯″紡     鑼冨洿 [0-1]    榛樿锛�0 鍙€夊弬鏁颁负锛歔0锛氫笉杈撳嚭娑堥殣淇″彿,1锛氳緭鍑烘秷闅愪俊鍙穄
                                                                                //                  閫氬父閮借缃负0锛屽鏋滀娇鐢–H32V307鐨凞VP鎺ュ彛鎴朣TM32鐨凞CMI鎺ュ彛閲囬泦闇€瑕佽缃负1
                                                                                //                  浠呮€婚捇椋� MT9V034 V1.5 浠ュ強浠ヤ笂鐗堟湰鏀寔璇ュ懡浠�

// 鎽勫儚澶村懡浠ゆ灇涓�
typedef enum
{
    MT9V03X_INIT = 0,                                                           // 鎽勫儚澶村垵濮嬪寲鍛戒护
    MT9V03X_AUTO_EXP,                                                           // 鑷姩鏇濆厜鍛戒护
    MT9V03X_EXP_TIME,                                                           // 鏇濆厜鏃堕棿鍛戒护
    MT9V03X_FPS,                                                                // 鎽勫儚澶村抚鐜囧懡浠�
    MT9V03X_SET_COL,                                                            // 鍥惧儚鍒楀懡浠�
    MT9V03X_SET_ROW,                                                            // 鍥惧儚琛屽懡浠�
    MT9V03X_LR_OFFSET,                                                          // 鍥惧儚宸﹀彸鍋忕Щ鍛戒护
    MT9V03X_UD_OFFSET,                                                          // 鍥惧儚涓婁笅鍋忕Щ鍛戒护
    MT9V03X_GAIN,                                                               // 鍥惧儚鍋忕Щ鍛戒护
    MT9V03X_PCLK_MODE,                                                          // 鍍忕礌鏃堕挓妯″紡鍛戒护(浠呮€婚捇椋嶮T9V034 V1.5浠ュ強浠ヤ笂鐗堟湰鏀寔璇ュ懡浠�)
    MT9V03X_CONFIG_FINISH,                                                      // 闈炲懡浠や綅锛屼富瑕佺敤鏉ュ崰浣嶈鏁�

    MT9V03X_COLOR_GET_WHO_AM_I = 0xEF,
    MT9V03X_SET_EXP_TIME = 0XF0,                                                // 鍗曠嫭璁剧疆鏇濆厜鏃堕棿鍛戒护
    MT9V03X_GET_STATUS,                                                         // 鑾峰彇鎽勫儚澶撮厤缃懡浠�
    MT9V03X_GET_VERSION,                                                        // 鍥轰欢鐗堟湰鍙峰懡浠�

    MT9V03X_SET_ADDR = 0XFE,                                                    // 瀵勫瓨鍣ㄥ湴鍧€鍛戒护
    MT9V03X_SET_DATA                                                            // 瀵勫瓨鍣ㄦ暟鎹懡浠�
}m9v03x_cmd_enum;

// 鎽勫儚澶存帴鍙ｇ被鍨嬫灇涓�
typedef enum
{
    MT9V03X_UART,
    MT9V03X_SCCB,
}m9v03x_type_enum;

extern vuint8   mt9v03x_finish_flag;                                            // 涓€鍦哄浘鍍忛噰闆嗗畬鎴愭爣蹇椾綅
extern uint8    mt9v03x_image[MT9V03X_H][MT9V03X_W];

uint16      mt9v03x_get_version         (void);
uint8       mt9v03x_set_exposure_time   (uint16 light);
uint8       mt9v03x_set_reg             (uint8 addr, uint16 data);

uint8       mt9v03x_init                (void);

#endif
